
core.form.Button = function(aParams){
    if(!aParams)
        aParams = {};
    try{
        this.init(aParams);
    }catch(e){
        
    }
}

core.BUTTON_RIGHT_WIDTH = 4;
core.BUTTON_HEIGHT = 20;

core.extendClass('core.form.Button', 'core.form.UIControl');

core.form.Button.prototype.setDisabled = function(aDisabled){
	var doDisable = this.disabled!=aDisabled;
	this.disabled = aDisabled;
	if(!doDisable)
		return;
	if(aDisabled){
		this.dDiv = core.newDiv(this.div, 'disabled');
		$(this.dDiv).width(this.minWidth);
		$(this.dDiv).mousedown(function(){
			return false;
		})
		$(this.dDiv).height(this.minHeight);
	}else{
		if(this.dDiv)
			$(this.dDiv).remove();
		this.dDiv = null;
	}
	this.refresh();
}

core.form.Button.prototype.init = function(aParams){
	this.xType = 'button';
    var events = $.makeArray(aParams.eventHandlers);
    if(aParams.handler){
        events.push({
            event: 'press',
            handler: aParams.handler
        });
        aParams.eventHandlers  = events;
    }
	this.superCall('init', arguments, aParams);
    this.tooltip = aParams.tooltip;
	this.text = aParams.text;
	this.icon = aParams.icon;
	this.minWidth = aParams.minWidth;
    if(!this.minWidth)
        this.minWidth = this.text?BUTTON_WIDTH:BUTTON_WIDTH_ICON;
	this.minHeight = core.BUTTON_HEIGHT;

    if(aParams.toolbarStyle){
        aParams.cls = 'button_toolbar';
        aParams.bgCls = 'button_bg_toolbar';

    }

	this.buttonDiv = core.newDiv(this.div, 'button');
    if(aParams.cls){
        $(this.buttonDiv).addClass(aParams.cls);
    }
    core.ttip.enableTooltip(this, this.buttonDiv);
    $(this.buttonDiv).width(this.minWidth).height(this.minHeight);
    this.buttonLeft = core.newDiv(this.buttonDiv, 'button_bg');
    this.buttonRight = core.newDiv(this.buttonDiv, 'button_bg');
    if(aParams.bgCls){
        $(this.buttonLeft).addClass(aParams.bgCls);
        $(this.buttonRight).addClass(aParams.bgCls);
    }
    $(this.buttonLeft).addClass('button_left').html('');
    $(this.buttonRight).addClass('button_right').html('');
	this.captionDiv = core.newDiv(this.buttonDiv, 'button_caption');
    
	var instance = this;
	var clEvent = function(e){
		if(instance.disabled)
			return false;
		return instance.fire('press', e);
	}
	
	$(this.buttonDiv).mousedown(function(e){
		if(instance.disabled)
			return false;
		$(this).addClass('button_pressed');
		instance.fire('focus', e);
		return false;
	});
	
	$(this.buttonDiv).mouseup(function(e){
		if(instance.disabled)
			return false;
		$(this).removeClass('button_pressed');
		return false;
	});
	
	$(this.buttonDiv).mouseout(function(e){
		if(instance.disabled)
			return false;
		$(this).removeClass('button_pressed');
		return false;
	});
	$(this.buttonDiv).click(clEvent);

    if(aParams.menu){
        this.menu = aParams.menu;
        this.on('press', function(){
            //Show menu here
            if(!instance.ddMenu){
                instance.ddMenu = core.showMenu(instance, instance.menu, true, false);
                instance.ddMenu.on('select', function(i, e, item){
                    instance.ddMenu = null;
                    instance.fire('menu_select', e, item);
                    return true;
                });
            }else{
                instance.ddMenu.hideMenu();
                instance.ddMenu = null;
            }
        });
    }
    if(aParams.disabled)
        this.setDisabled(true);
}

core.form.Button.prototype.setIcon = function(aIcon){
    this.icon = aIcon;
    this.refresh();
}

core.form.Button.prototype.setText = function(aText){
    this.params.text = aText;
    this.refresh();
}

core.form.Button.prototype.makeResize = function(aX, aY, aWidth, aHeight){
	$(this.captionDiv).html(this.imagesToHTMLSprite(this.icon)+(this.params.text || ''));
	this.alignBlock(this.buttonDiv, $(this.div).width(), $(this.div).height());
	if(this.dDiv){
//		core.log('align disabled block', this.dDiv, $(this.div).innerWidth(), $(this.div).innerHeight());
		this.alignBlock(this.dDiv, $(this.div).innerWidth(), $(this.div).innerHeight());
	}
	this.alignBlock(this.captionDiv, this.minWidth, this.minHeight, 'center', 'middle');
}

core.form.Button.prototype.doFocus = function(){
	$(this.buttonLeft).addClass('button_selected_left');
	$(this.buttonRight).addClass('button_selected_right');
    this.fire('focus');
	return true;
}

core.form.Button.prototype.doUnFocus = function(){
	$(this.buttonLeft).removeClass('button_selected_left');
	$(this.buttonRight).removeClass('button_selected_right');
}

core.form.Button.prototype.doKeyPress = function(e){
	if(this.disabled)
		return true;
	if(e.which==32 || e.which==10 || e.which==13){
		this.fire('press', e);
		return false;
	}
	return true;
}
